//图片上传
function imageUploadEdit(thatFile, thatUrl) {
    //处理预览数据
    var urlObj = $(thatUrl).val();
    var previewUrl = [];
    var config = [];
    var previewKeys = [];
    if (urlObj) {
        urlObj = JSON.parse(urlObj);
        for (var key in urlObj) {
            previewUrl.push(urlObj[key]);
            var tem = {};
            tem['key'] = key;
            config.push(tem);
            previewKeys.push(key);
        }
    };

    //图片上传
    var params = {
        'theme': 'explorer-fas',
        'uploadUrl': ctx + 'common/uploadImg',
        'deleteUrl': '#',
        showUpload: false,
        overwriteInitial: false,
        initialPreviewAsData: true,
        language: 'zh',
        uploadAsync: true,
        dropZoneEnabled: false,
        maxFileCount: 10,
        resizeImage: false,
        showCaption: false,
        showPreview: true,
        browseClass: "btn btn-primary btn-lg",
        allowedFileExtensions: ['jpg', 'png', 'gif', 'jpeg', 'bmp'],
        previewFileIcon: "",
        initialPreviewConfig: config,
        initialPreview: previewUrl
    };
    var lastThatFile =
        $(thatFile).fileinput(params)
            .on("filebatchselected", function (event, data) {
                //选择即上传
                $(thatFile).fileinput("upload");
            })
            .on('fileuploaded', function (event, data, previewId, index) {
                //处理上传后数据，上传后插件会将现有上传文件previewId重排，，此时需对现有地址数据做重排处理，此时有预览数据，预览数据不做处理
                let count = 0;
                let imgUrl = {};
                if ($(thatUrl).val()) {
                    let temp = JSON.parse($(thatUrl).val());
                    for(let key in temp){
                        if (previewKeys.indexOf(key) == -1){
                            imgUrl[previewId.substr(0, previewId.length - 1) + count] = temp[key];
                            count++;
                        }else {
                            imgUrl[key] = temp[key];
                        }
                    }
                };
                previewId = previewId.substr(0, previewId.length - 1) + count;
                imgUrl[previewId] = data.response.url;
                $(thatUrl).val(JSON.stringify(imgUrl));
            })
            .on('filesuccessremove', function (event, previewId) {
                //点击缩略图中删除按钮触发此事件（此事件是新上传图片缩略图触发）
                var imgUrl = JSON.parse($(thatUrl).val());
                delete imgUrl[previewId];
                $(thatUrl).val(JSON.stringify(imgUrl));
            })
            .on('filedeleted', function (event, key) {
                //点击缩略图中删除按钮触发此事件（此事件是初始化预览图片缩略图触发）
                var tem = JSON.parse($(thatUrl).val());
                delete tem[key];
                let index = previewKeys.indexOf(key);
                if (index != -1)
                    delete previewKeys[index];
                $(thatUrl).val(JSON.stringify(tem));
            })
            .on('filecleared', function (event) {
                //点击左下角移除或者右上角×号按钮触发此事件，此时需把初始化预览图片置空
                $(thatUrl).val("");
                $(thatFile).fileinput('refresh', {initialPreview: []});
            });
}